﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Read.Applications.Commands.Audit;
using AuthCenter_API.Read.Dtos;
using MediatR;

namespace AuthCenter_API.Read.Applications.CommandHandlers.Audit
{
    public class AuditQueryCommandHandler : IRequestHandler<AuditQueryCommand, APIResult<APIPaging<AuditDto>>>
    {
        private readonly IBaseRepository<ShenPiModel> _shenpiRepository;
        private readonly IBaseRepository<DepModel> _depRepository;
        private readonly IBaseRepository<UserRoleModel> _URRepository;
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IBaseRepository<UserModel> _userRepository;
        public AuditQueryCommandHandler(IBaseRepository<ShenPiModel> shenpiRepository, IBaseRepository<DepModel> depRepository, IBaseRepository<UserRoleModel> uRRepository, IBaseRepository<RoleModel> roleRepository, IBaseRepository<UserModel> userRepository)
        {
            _shenpiRepository = shenpiRepository;
            _depRepository = depRepository;
            _URRepository = uRRepository;
            _roleRepository = roleRepository;
            _userRepository = userRepository;
        }

        public Task<APIResult<APIPaging<AuditDto>>> Handle(AuditQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<AuditDto>> result = new APIResult<APIPaging<AuditDto>>();
            var ridList=request.RoleId.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            var excuseList = _shenpiRepository.GetAll().ToList();
            if (ridList.Contains(1007))
            {
                excuseList = excuseList.Where(x => x.DepId == request.DeptId).ToList();
            }
            else
            {
                excuseList = excuseList.Where(x => x.Uid == request.Id).ToList();
            }
            result.Code = APIEnum.QuerySuccess;
            result.Message = APIEnum.QuerySuccess.Desc();
            var m = from a in excuseList
                    join b in _depRepository.GetAll()
                    on a.DepId equals b.DepId
                    where a.IsDel==0
                    select new AuditDto
                    {
                        ShenId = a.ShenId,
                        UserName = a.UserName,
                        DepId = a.DepId,
                        Type = a.Type,
                        StartTime = a.StartTime,
                        EndTime = a.EndTime,
                        BaoDaoTime = a.BaoDaoTime,
                        Desc = a.Desc,
                        LingDao = a.LingDao,
                        State = a.State,
                        DepName = b.DepName,
                        CreateTime=a.CreateTime
                    };
            if(request.StartTime!=null)
            {
                m = m.Where(x => x.StartTime<=request.StartTime);
            }
            if(request.EndTime != null)
            {
                m = m.Where(x => x.EndTime >= request.EndTime);
            }
            if(!string.IsNullOrEmpty(request.UserName))
            {
                m=m.Where(x=>x.UserName.Contains(request.UserName));
            }
            if(request.State!=null)
            {
                m = m.Where(x => x.State == request.State);
            }
            var count = m.Count();
            var page = (int)Math.Ceiling(1.0 * count / request.Size);
            m = m.OrderByDescending(x => x.ShenId).Skip((request.Index - 1) * request.Size).Take(request.Size);
            APIPaging<AuditDto> p = new APIPaging<AuditDto>();
            p.TotalPage = page;
            p.TotalCount = count;
            p.List = m.ToList();
            result.Data = p;
            return Task.FromResult(result);
        }
    }
}
